package com.atguigu.crowd.service.impl;

import com.atguigu.crowd.config.PO.Address;
import com.atguigu.crowd.config.PO.AddressExample;
import com.atguigu.crowd.config.PO.Order;
import com.atguigu.crowd.config.PO.OrderProject;
import com.atguigu.crowd.config.VO.AddressVO;
import com.atguigu.crowd.config.VO.OrderProjectVO;
import com.atguigu.crowd.config.VO.OrderVO;
import com.atguigu.crowd.mapper.AddressMapper;
import com.atguigu.crowd.mapper.OrderMapper;
import com.atguigu.crowd.mapper.OrderProjectMapper;
import com.atguigu.crowd.service.OrderService;
//import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderProjectMapper orderProjectMapper;

    @Autowired
    private AddressMapper addressMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public OrderProjectVO getOrderProjectVO(Integer projectId, Integer returnId) {
        return orderProjectMapper.selectOrderProjectVO(returnId);
    }

    @Override
    public List<AddressVO> getAddressVOList(Integer memberId) {
        AddressExample example = new AddressExample();
        example.createCriteria().andMemberIdEqualTo(memberId);
        List<Address> addressList = addressMapper.selectByExample(example);
        List<AddressVO> addressVOList = new ArrayList<>();
        for (Address addressPO : addressList) {
            AddressVO addressVO = new AddressVO();
            BeanUtils.copyProperties(addressPO, addressVO);
            addressVOList.add(addressVO);
        }
        return addressVOList;
    }

//    @GlobalTransactional
    @Override
    public void saveAddress(AddressVO addressVO) {
        Address address = new Address();
        BeanUtils.copyProperties(addressVO, address);
        addressMapper.insert(address);
    }

//    @GlobalTransactional
    @Override
    public void saveOrder(OrderVO orderVO) {
        Order order = new Order();
        BeanUtils.copyProperties(orderVO, order);
        OrderProject orderProject = new OrderProject();
        BeanUtils.copyProperties(orderVO.getOrderProjectVO(), orderProject);
        orderMapper.insert(order);
        Integer id = order.getId();
        orderProject.setOrderId(id);
        orderProjectMapper.insert(orderProject);
    }
}
